
class Sheet:
    def __init__(self, name=None):
        self.name = name
        self.rows = []
    
    def clear(self):
        self.name = ""
        self.rows.clear()        
    def copy(self, sheet):
        self.clear()
        self.name = sheet.name
        for row in sheet.rows:
            self.rows.append(row.copy())
        return self
    
    def insert_row(self, index, row):
        self.rows.insert(index, row)
    
    def remove_row(self, index):
        self.rows.remove(index)
                
    def append_row(self, row):
        self.rows.append(row)       
    
    @staticmethod
    def Copy(sheet):
        newsheet = Sheet()
        newsheet.copy(sheet)
        return newsheet

class DataSheet:
    def __init__(self, filename = ""):
        self.Sheets = []
        self.source_file_path = filename
        self.current_sheet = None
    
    def set_current_sheet_by_index(self, index):
        if index >= len(self.Sheets): 
            raise ValueError(f"[DataSheet] not found sheet indexed as : {index}")
        else:
            self.current_sheet = self.Sheets[index]

    def set_current_sheet_by_name(self, name):
        thesheet = next((p for p in self.Sheets if p.name == name), None)       
        if not thesheet :
            raise ValueError(f"[DataSheet] not found sheet named as : {name}")
        else:             
            self.current_sheet = thesheet # 附个对象
    
    def clearSheets(self):
        self.Sheets.clear()
        self.current_sheet = None
    
    def insert_sheet(self, index, sheet):
        self.Sheets.insert(index, sheet)
        
    def remove_sheet(self, sheet):
        self.Sheets.remove(sheet)
        
    def append_sheet(self, sheet):
        self.Sheets.append(sheet)
        
    def copy(self, datasheet):
        self.clearSheets()
        self.source_file_path = datasheet.source_file_path
        for shhet in datasheet.Sheets:
            self.Sheets.append(Sheet.Copy(shhet))
        return self
    @staticmethod
    def Copy(datasheet):
        newDataSheet = DataSheet()
        newDataSheet.copy(datasheet)
        return newDataSheet
        
